@echo off
echo ========================================
echo LimeReader Installer Creator
echo ========================================
echo.

:: Find executable
set "EXE_NAME=LimeReader.exe"
set "BUILD_DIR="

if exist "build\%EXE_NAME%" set "BUILD_DIR=build"
if exist "build\Release\%EXE_NAME%" set "BUILD_DIR=build\Release"
if exist "build\bin\Release\%EXE_NAME%" set "BUILD_DIR=build\bin\Release"
if exist "bin\%EXE_NAME%" set "BUILD_DIR=bin"

if "%BUILD_DIR%"=="" (
    echo [ERROR] Cannot find %EXE_NAME%
    echo Please compile the project first!
    pause
    exit /b 1
)

echo [OK] Found: %BUILD_DIR%\%EXE_NAME%
echo.

:: Set Qt path
set "QT_DIR=C:\Qt\6.9.2\mingw_64"
if not exist "%QT_DIR%" (
    echo [ERROR] Qt not found at: %QT_DIR%
    echo Please update QT_DIR in this script
    pause
    exit /b 1
)

echo [OK] Qt path: %QT_DIR%
echo.

:: Create deploy directory
set "DEPLOY_DIR=%BUILD_DIR%"
echo Deploy directory: %DEPLOY_DIR%
echo.

:: Run windeployqt
echo [Step 1] Running windeployqt...
"%QT_DIR%\bin\windeployqt.exe" --release --no-compiler-runtime "%DEPLOY_DIR%\%EXE_NAME%"
if errorlevel 1 (
    echo [WARNING] windeployqt failed
)
echo.

:: Copy MinGW runtime
echo [Step 2] Copying MinGW runtime...
copy /y "%QT_DIR%\bin\libgcc_s_seh-1.dll" "%DEPLOY_DIR%\" >nul 2>&1
copy /y "%QT_DIR%\bin\libstdc++-6.dll" "%DEPLOY_DIR%\" >nul 2>&1
copy /y "%QT_DIR%\bin\libwinpthread-1.dll" "%DEPLOY_DIR%\" >nul 2>&1
echo [OK]
echo.

:: Copy Poppler DLLs
echo [Step 3] Copying Poppler DLLs...
if exist "mingw64-poppler\bin\libpoppler-153.dll" (
    copy /y "mingw64-poppler\bin\*.dll" "%DEPLOY_DIR%\" >nul 2>&1
    echo [OK] Poppler DLLs copied
) else (
    echo [WARNING] Poppler DLLs not found
)
echo.

:: Copy translations
echo [Step 4] Copying translations...
if exist "translations" (
    if not exist "%DEPLOY_DIR%\translations" mkdir "%DEPLOY_DIR%\translations"
    xcopy /y /s /q "translations\*.qm" "%DEPLOY_DIR%\translations\" >nul 2>&1
    echo [OK]
) else (
    echo [SKIP] No translations folder
)
echo.

:: Copy resources
echo [Step 5] Copying resources...
if exist "icons" (
    xcopy /y /s /q /i "icons" "%DEPLOY_DIR%\icons\" >nul 2>&1
)
if exist "images" (
    xcopy /y /s /q /i "images" "%DEPLOY_DIR%\images\" >nul 2>&1
)
echo [OK]
echo.

:: List DLLs
echo [Step 6] Checking DLLs...
echo.
dir /b "%DEPLOY_DIR%\*.dll"
echo.

:: Check Inno Setup
echo [Step 7] Looking for Inno Setup...
set "ISCC="
if exist "C:\Program Files (x86)\Inno Setup 6\ISCC.exe" (
    set "ISCC=C:\Program Files (x86)\Inno Setup 6\ISCC.exe"
)
if exist "C:\Program Files\Inno Setup 6\ISCC.exe" (
    set "ISCC=C:\Program Files\Inno Setup 6\ISCC.exe"
)

if "%ISCC%"=="" (
    echo [WARNING] Inno Setup not found
    echo Please compile LimeReader.iss manually
    echo Download: https://jrsoftware.org/isinfo.php
    goto :end
)

echo [OK] Found: %ISCC%
echo.

:: Compile installer
echo [Step 8] Compiling installer...
echo Using English-only version to avoid encoding issues...
"%ISCC%" "LimeReader_EN.iss"
if errorlevel 1 (
    echo [ERROR] Installer compilation failed
    pause
    exit /b 1
)

echo.
echo ========================================
echo SUCCESS! Installer created!
echo ========================================
echo.
if exist "installer\LimeReader_Setup_v2.7.2.exe" (
    echo Output: installer\LimeReader_Setup_v2.7.2.exe
    dir "installer\*.exe"
)

:end
echo.
pause

